package com.hk.yajin.function.index;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseFragment;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.IndexDataBean;
import com.hk.yajin.model.IndexModel;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 伦哥哥 on 2017/6/29.
 */

public class MenuIndexFragment extends BaseFragment {
    private Toolbar toolbar;
    protected List<IndexModel> dataList = new ArrayList<>();
    protected RecyclerView mRecyclerView;
    protected IndexMultQuickAdapter mQuickAdapter;
    protected SwipeRefreshLayout swipe_container;
    private IndexDataBean dataBean;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        mContentView = inflater.inflate(R.layout.fragment_menu_index, container, false);
        initToolBar();
        initSwipeView();
        initAdapter();
        initRecyclerView();
        return mContentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!isCreated) {
            initDataList();
            isCreated = true;
        }
    }

    protected void initSwipeView() {
        swipe_container = (SwipeRefreshLayout) mContentView.findViewById(R.id.swipe_container);
        swipe_container.setEnabled(true);

        swipe_container.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mQuickAdapter.removeAllFooterView();
                initDataList();
            }
        });

    }


    private void handleData() {
        dataList.clear();
        if (dataBean.getMail() != null && dataBean.getMail().size() > 0) {
            dataList.add(new IndexModel(0, dataBean.getMail()));
        }
        if (dataBean.getArticles() != null && dataBean.getArticles().size() > 0) {
            dataList.add(new IndexModel(1, dataBean.getArticles()));
        }
        if (dataBean.getVideo() != null && dataBean.getVideo().size() > 0) {
            dataList.add(new IndexModel(2, dataBean.getVideo()));
        }
        if (dataBean.getView() != null && dataBean.getView().size() > 0) {
            dataList.add(new IndexModel(3, dataBean.getView()));
        }

        if(dataList!=null&&dataList.size()>0){
            mQuickAdapter.notifyDataSetChanged();
        }else{
            showDefaultView_NO_DTTA();
        }



    }

    @Override
    public void defaultViewClick(int tag) {
        hideDefaultView();
        initDataList();

    }

    private void initDataList() {
        isNetWorkConnect();
        showLoadingView();
        Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
        HttpRequestUtils.post(true, UrlConstants.FUNCTION_INDEX, reqParam, false, new HttpCallBack() {
            @Override
            public void onSucced(String data) {
                hideLoadingView();
                swipe_container.setRefreshing(false);
                dataBean = FastjsonUtils.getBeanObject(data, IndexDataBean.class);
                if (dataBean != null) {
                    handleData();
                } else {
                    showDefaultView_NO_DTTA();
                }

            }

            @Override
            public void onBadHttp(String tip) {
                hideLoadingView();
                swipe_container.setRefreshing(false);
                showDefaultView_API_ERROR();
                showShortToast(tip);
            }

            @Override
            public void onErrorCode(NewWorkBean bean) {
                hideLoadingView();
                swipe_container.setRefreshing(false);
                showDefaultView_NO_DTTA();
                showShortToast(bean.getMsg());
            }
        });

    }


    protected void initAdapter() {
        mQuickAdapter = new IndexMultQuickAdapter(dataList, mContext);
        mQuickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mQuickAdapter.isFirstOnly(true);
        mQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                initDataList();
            }
        }, mRecyclerView);
        // mQuickAdapter.addHeaderView(getActivity().getLayoutInflater().inflate(R.layout.head_view, (ViewGroup) mRecyclerView.getParent(), false));
        mQuickAdapter.setEnableLoadMore(false);
    }

    protected void initRecyclerView() {
        mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.rv_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mQuickAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {

            }
        });

    }

    private void initToolBar() {
        toolbar = (Toolbar) mContentView.findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(UiUtil.getColor(R.color.white));
        toolbar.setTitle("");
        TextView textView = (TextView) mContentView.findViewById(R.id.tv_title);
        textView.setText(UiUtil.getString(R.string.menu_one));
        textView.setTextColor(UiUtil.getColor(R.color.black_282828));
    }
}
